package com.bw.controller;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.bw.service.EduService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author 小郭同学
 * * @date 2025/2/19
 */
@RestController
public class EduController {
    @Autowired
    EduService eduService;
    @RequestMapping("/userCount")
    public Object userCount(){
        List<Map> list = eduService.userCount();
        //1
        JSONObject jsonObject = new JSONObject();
        //1.1
        jsonObject.put("status",0);
        //1.2
        jsonObject.put("msg","");
        //1.3
        JSONObject dataObject = new JSONObject();


        JSONArray categoriesArray = new JSONArray();
        for (Map map : list) {
            categoriesArray.add(map.get("time"));
        }
        dataObject.put("categories",categoriesArray);

        JSONArray seriesArray = new JSONArray();
        JSONObject seriesObject = new JSONObject();
        seriesObject.put("name","独立访客数");
        JSONArray dataArray = new JSONArray();
        for (Map map : list) {
            dataArray.add(map.get("num"));
        }
        seriesObject.put("data",dataArray);
        seriesArray.add(seriesObject);
        dataObject.put("series",seriesArray);


        jsonObject.put("data",dataObject);
        return jsonObject;
    }

    @RequestMapping("/sourceTotal")
    public Object sourceTotal(String newDate){
        List<Map> list = eduService.sourceTotal(newDate);
        //1
        JSONObject jsonObject = new JSONObject();
        //1.1
        jsonObject.put("status",0);
        //1.2
        jsonObject.put("msg","");
        //1.3
        JSONObject dataObject = new JSONObject();
        JSONArray columnsArray = new JSONArray();
        JSONObject columnsObject1 = new JSONObject();
        columnsObject1.put("name","来源名称");
        columnsObject1.put("id","name");
        JSONObject columnsObject2 = new JSONObject();
        columnsObject2.put("name","订单总额");
        columnsObject2.put("id","total");
        JSONObject columnsObject3 = new JSONObject();
        columnsObject3.put("name","下单用户总数");
        columnsObject3.put("id","users");
        JSONObject columnsObject4 = new JSONObject();
        columnsObject4.put("name","订单总数");
        columnsObject4.put("id","orders");
        columnsArray.add(columnsObject1);
        columnsArray.add(columnsObject2);
        columnsArray.add(columnsObject3);
        columnsArray.add(columnsObject4);
        dataObject.put("columns",columnsArray);

        JSONArray rowsArray = new JSONArray();
        for (Map map : list) {
            JSONObject rowsObject = new JSONObject();
            rowsObject.put("name",map.get("name"));
            rowsObject.put("total",map.get("total"));
            rowsObject.put("users",map.get("users"));
            rowsObject.put("orders",map.get("orders"));
            rowsArray.add(rowsObject);
        }
        dataObject.put("rows",rowsArray);


        jsonObject.put("data",dataObject);
        return jsonObject;
    }

    @RequestMapping("/newUser")
    public Object newUser(String newDate){
        List<Map> list = eduService.newUser(newDate);
        //1
        JSONObject jsonObject = new JSONObject();
        //1.1
        jsonObject.put("status",0);
        //1.2
        jsonObject.put("msg","");
        //1.3
        jsonObject.put("data",list.get(0).get("users"));
        return jsonObject;
    }

    @RequestMapping("/orderCount")
    public Object orderCount(String newDate){
        List<Map> list = eduService.orderCount(newDate);
        //1
        JSONObject jsonObject = new JSONObject();
        //1.1
        jsonObject.put("status",0);
        //1.2
        jsonObject.put("msg","");
        //1.3
        JSONObject dataObject = new JSONObject();
        JSONArray columnsArray = new JSONArray();
        JSONObject columnsObject1 = new JSONObject();
        columnsObject1.put("name","学科名称");
        columnsObject1.put("id","subjectName");
        JSONObject columnsObject2 = new JSONObject();
        columnsObject2.put("name","类别名称");
        columnsObject2.put("id","categoryName");
        JSONObject columnsObject3 = new JSONObject();
        columnsObject3.put("name","课程名称");
        columnsObject3.put("id","courseName");
        JSONObject columnsObject4 = new JSONObject();
        columnsObject4.put("name","下单金额");
        columnsObject4.put("id","total");
        columnsArray.add(columnsObject1);
        columnsArray.add(columnsObject2);
        columnsArray.add(columnsObject3);
        columnsArray.add(columnsObject4);
        dataObject.put("columns",columnsArray);

        JSONArray rowsArray = new JSONArray();
        for (Map map : list) {
            JSONObject rowsObject = new JSONObject();
            rowsObject.put("subjectName",map.get("subjectName"));
            rowsObject.put("categoryName",map.get("categoryName"));
            rowsObject.put("courseName",map.get("courseName"));
            rowsObject.put("total",map.get("total"));
            rowsArray.add(rowsObject);
        }
        dataObject.put("rows",rowsArray);

        jsonObject.put("data",dataObject);
        return jsonObject;
    }
}
